package com.hieuzest.citsaw;


import android.util.Log;

import com.hieuzest.citsaw.beam.CiticService;
import com.hieuzest.citsaw.beam.Jigsaw;
import com.hieuzest.citsaw.beam.Piece;
import com.hieuzest.citsaw.beam.RequestBody;
import com.hieuzest.citsaw.beam.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

import static java.lang.Thread.sleep;

/**
 * Created by Giraffe on 2017/11/26.
 */

public class JigsawManager {
    private static JigsawManager instance;

    private int mUid = -1;
    private ArrayList<Jigsaw> mJigsaws = new ArrayList<>();
    private ArrayList<Piece> mNearbyPieces = new ArrayList<>();
    private ArrayList<CiticService> mServices = new ArrayList<>();

    private JigsawManager() {
        instance = this;
    }

    public boolean login(int uid) {
        if (mUid != -1) return false;
        mUid = uid;
        Observable.create(new Observable.OnSubscribe<Object>() {
            @Override
            public void call(Subscriber<? super Object> subscriber) {
                loop();
            }
        }).subscribeOn(Schedulers.io()).subscribe();
        return true;
    }

    public List<Jigsaw> getJigsaws() {
        synchronized (mJigsaws) {
            return new ArrayList<>(mJigsaws);
        }
    }

    public List<Piece> getNearbyPieces() {
        synchronized (mNearbyPieces) {
            return new ArrayList<>(mNearbyPieces);
        }
    }

    public List<CiticService> getServices() {
        synchronized (mServices) {
            return new ArrayList<>(mServices);
        }
    }

    public List<Piece> getCaughtPieces(int jid) {
        ArrayList<Piece> l = new ArrayList<>();
        for(Piece p : mJigsaws.get(jid).pieces) {
            if (p.status == Piece.STATUS_FOUND && p.owner == mUid) l.add(p);
        }
        return l;
    }

    public List<Piece> getMarkedPieces(int jid) {
        ArrayList<Piece> l = new ArrayList<>();
        for(Piece p : mJigsaws.get(jid).pieces) {
            if (p.status == Piece.STATUS_MARKED && p.owner == mUid) l.add(p);
        }
        return l;
    }

    private void loop() {
        while (true) {
            try {
                Call<ResponseBody> call = WebService.service.getAllInfo(RequestBody.generate());
                call.enqueue(new Callback<ResponseBody>() {
                                 @Override
                                 public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                                     if (response.body().jigsaws != null && response.body().jigsaws.length > 0)
                                     synchronized (mJigsaws) {
                                         mJigsaws.clear();
                                         mJigsaws.addAll(Arrays.asList(response.body().jigsaws));
                                     }
                                     if (response.body().nearbyPieces != null && response.body().nearbyPieces.length > 0)
                                     synchronized (mNearbyPieces) {
                                         Log.i("MANAGER", "NEARBY");
                                         mNearbyPieces.clear();
                                         mNearbyPieces.addAll(Arrays.asList(response.body().nearbyPieces));
                                     }
                                     if (response.body().services != null && response.body().services.length > 0)
                                     synchronized (mServices) {
                                         mServices.clear();
                                         mServices.addAll(Arrays.asList(response.body().services));
                                     }
                                 }

                                 @Override
                                 public void onFailure(Call<ResponseBody> call, Throwable t) {
                                     t.printStackTrace();
                                 }
                             });
                        sleep(1000);
            }catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    public static JigsawManager getInstance() {
        if (instance == null) return instance = new JigsawManager();
        return instance;
    }
}
